package com.prs.crm.dao.stock.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.stock.StoreRoomDao;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.store.StoreRoom;

public class StoreRoomDaoImpl extends BaseDaoImpl<StoreRoom> implements
		StoreRoomDao {

	@SuppressWarnings("unchecked")
	public List<StoreRoom> getAllStoreRoom() {
		return (List<StoreRoom>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(StoreRoom.class);

						criteria.addOrder(Order.asc("id"));

						return criteria.list();
					}
				});

	}

	@Override
	public void saveOrStoreRoom(StoreRoom room) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(room);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public StoreRoom getRoomBycode(final String code) {
		// TODO Auto-generated method stub
	
		 return (StoreRoom) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(StoreRoom.class);
				        	criteria.add(Restrictions.eq("roomcode", code));
				        	List<StoreRoom> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}

				        	
				        }
					}
			);
	}



}
